package com.haochang.chunkvod.model.cash;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.haochang.base.BaseActivity;
import com.haochang.chunkvod.app.config.ApiConfig;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.tools.thirdparty.network.http.httpenum.HttpCacheEnum;
import com.haochang.chunkvod.tools.thirdparty.network.http.httpenum.HttpMethodEnum;
import com.haochang.chunkvod.tools.thirdparty.network.http.httpenum.HttpRequestLoadingEnum;
import com.haochang.chunkvod.tools.thirdparty.network.http.request.HttpRequestBuilder;
import com.haochang.chunkvod.tools.thirdparty.network.http.request.HttpRequestTask;

/**
 *@author qintao
 *created at 2016/8/9 12:03
 * 交班设计到的数据请求
 */

public class VehicleData
{

	private BaseActivity context;

	public VehicleData(BaseActivity context)
	{
		this.context = context;
	}

	public interface IShiftQueryListener
	{
		public void onRequestSuccess(VehicleInfo vehicleInfo);
	}

	public interface IShiftDetailsListener
	{
		public void onRequestSuccess(JSONObject jsonObject);
	}

	/**
	 * 交班查询
	 * @param listener
	 */
	public void requestShiftQuery(final IShiftQueryListener listener)
	{
		HttpRequestTask httpRequestTask = new HttpRequestBuilder(context).interfaceName(ApiConfig.SHIFT_QUERY).param(null).httpMethodEnum(HttpMethodEnum.GET)
				.localCache(HttpCacheEnum.ENABLE_VALID).httpRequestLoadingEnum(HttpRequestLoadingEnum.HTTP_LOADING_DEFAULT)
				.httpRequestSucessListener(new HttpRequestBuilder.IHttpRequestSucessListener()
				{

					@Override
					public void onResponseSucess(JSONObject jsonObject)
					{
						if (jsonObject != null)
						{
							if (listener != null)
							{
								listener.onRequestSuccess(parseShiftQueryData(jsonObject));
							}
						}
					}
				}).httpRequestFailedListener(new HttpRequestBuilder.IHttpRequestFailedListener()
				{

					@Override
					public void onError(int errno, String errMsg)
					{
						ToastUtils.showText(context, errMsg);
					}
				}).build();
		httpRequestTask.execute();
	}

	/**
	 * 交班详情
	 * @param listener
	 */
	public void requestShiftDetails(final IShiftDetailsListener listener)
	{
		HttpRequestTask httpRequestTask = new HttpRequestBuilder(context).interfaceName(ApiConfig.SHIFT_DETAIL).param(null).httpMethodEnum(HttpMethodEnum.GET)
				.localCache(HttpCacheEnum.ENABLE_VALID).httpRequestLoadingEnum(HttpRequestLoadingEnum.HTTP_LOADING_DEFAULT)
				.httpRequestSucessListener(new HttpRequestBuilder.IHttpRequestSucessListener()
				{

					@Override
					public void onResponseSucess(JSONObject jsonObject)
					{
						if (jsonObject != null)
						{
							if (listener != null)
							{
								listener.onRequestSuccess(jsonObject);
							}
						}
					}
				}).httpRequestFailedListener(new HttpRequestBuilder.IHttpRequestFailedListener()
				{

					@Override
					public void onError(int errno, String errMsg)
					{
						ToastUtils.showText(context, errMsg);
					}
				}).build();
		httpRequestTask.execute();
	}

	/**
	 * 解析班次查询信息
	 * @param jsonObject
	 * @return
	 */
	private VehicleInfo parseShiftQueryData(JSONObject jsonObject)
	{
		VehicleInfo info = new VehicleInfo();
		info.setShiftName(jsonObject.optString("shift"));
		info.setBusiness_date(jsonObject.optString("bussiness_date"));
		info.setBill_count(jsonObject.optInt("bill_count"));
		info.setReceivable_fee(jsonObject.optInt("receivable_fee"));
		info.setGuadan_fee(jsonObject.optInt("guadan_fee"));
		info.setPaid_fee(jsonObject.optInt("paied_fee"));
		info.setShift_count(jsonObject.optInt("shift_count"));
		JSONArray jsonArray = jsonObject.optJSONArray("bill_info");
		if (jsonArray != null && jsonArray.length() > 0)
		{
			List<BillInfo> infoList = new ArrayList<BillInfo>();
			for (int i = 0; i < jsonArray.length(); i++)
			{
				JSONObject jObject = jsonArray.optJSONObject(i);
				if (jObject == null)
					continue;
				BillInfo billInfo = new BillInfo();
				billInfo.setBillNum(jObject.optString("bill_code"));
				billInfo.setPend(jObject.optInt("guadan_fee"));
				billInfo.setReceivable(jObject.optInt("receivable_fee"));
				billInfo.setReceived(jObject.optInt("paied_fee"));
				infoList.add(billInfo);
			}
			info.setBillInfoList(infoList);
		}
		return info;
	}
}
